package neusoft.edu.cn.a215lmeordering.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.mob.MobSDK;

import cn.smssdk.EventHandler;
import neusoft.edu.cn.a215lmeordering.Listener.Listener;
import neusoft.edu.cn.a215lmeordering.R;
import neusoft.edu.cn.a215lmeordering.bean.Login;
import neusoft.edu.cn.a215lmeordering.bean.Register;
import neusoft.edu.cn.a215lmeordering.model.RegisterModel;

public class RegisterActivity extends AppCompatActivity {

    Button register;
    EditText user;
    EditText pass;
    EditText tel;
    EditText adr;
    EditText comment;

    String usertext = "";
    String passtext = "";
    String teltext = "";
    String adrtext = "";
    String commenttext = "";

    Listener registerListener = new Listener() {

        @Override
        public void onResponse(Object o) {
            Register register = (Register) o;
            if(register.getSuccess().equals("0")) {
                Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
            }else{
                final Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent);
                finish();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(RegisterActivity.this, "注册获取信息失败", Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        init();

        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                usertext = user.getText().toString();
                passtext = pass.getText().toString();
                teltext = tel.getText().toString();
                adrtext = adr.getText().toString();
                commenttext = comment.getText().toString();

                if(usertext.equals("")){
                    Toast.makeText(RegisterActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
                }else if(passtext.equals("")){
                    Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                }else if(teltext.equals("")){
                    Toast.makeText(RegisterActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show();
                }else if(adrtext.equals("")){
                    Toast.makeText(RegisterActivity.this, "请输入地址", Toast.LENGTH_SHORT).show();
                }else if(commenttext.equals("")){
                    Toast.makeText(RegisterActivity.this, "请输入身份，例如：老师", Toast.LENGTH_SHORT).show();
                }else{
                    RegisterModel registerModel = new RegisterModel();
                    registerModel.getResultList(usertext,passtext,teltext,adrtext,commenttext,registerListener);
                }

            }
        });

    }

    private void init() {
        register = findViewById(R.id.register);
        user = findViewById(R.id.editText5);
        pass = findViewById(R.id.editText6);
        tel = findViewById(R.id.editText7);
        adr = findViewById(R.id.editText8);
        comment = findViewById(R.id.editText9);
    }
}
